#define DEBUG
#include <cstdio>
#include <algorithm>

using namespace std;

typedef long long ll;
const int maxn=1000, maxm=50000;
const ll inf=0x7ffffffffffffff;

class Node {
public:
  int a, b, id;

  static bool cmp(Node &x, Node &y) {
    return x.a<y.a;
  }
};

class Network {
public:
  static const int maxno=maxn+maxm+2, maxsi=1000000;
  int n, m=1, ind[maxno+1], to[maxsi+2], link[maxsi+2], gap[maxno+1], dis[maxno+1];
  ll val[maxsi+2];
  bool end=false;

  void addTwoSide(int u, int v, ll w) {
    m++;
    to[m] = v, val[m] = w, link[m] = ind[u];
    ind[u] = m;
  }

  void addSide(int u, int v, ll w) {
    addTwoSide(u, v, w);
    addTwoSide(v, u, 0);
  }

  void init() {
    gap[0] = n;
  }

  ll run(int o, int t, ll flow) {
    if (o==t) return flow;
    ll done=0;
    for (int i=ind[o]; i; i=link[i]) {
      if (val[i] && dis[to[i]]+1==dis[o]) {
        ll temp=run(to[i], t, min(flow-done, val[i]));
        val[i]-=temp, val[i^1]+=temp, done+=temp;
        if (end || flow==done) return done;
      }
    }
    if (!(--gap[dis[o]])) end=true;
    gap[++dis[o]]++;
    return done;
  }
};

void buildNet(int n, Node nodes[], bool g[][maxn+1], Network &net) {
  static int b[maxn+1];
  for (int i=1; i<=n; i++) b[i]=nodes[i].b;
  sort(nodes+1, nodes+n+1, Node::cmp);

  net.n = n+2;
  for (int i=1; i<=n; i++) net.addSide(1, i+2, b[i]);

  static int ind[maxn+1];
  for (int i=1; i<=n; i++) ind[i] = 2;
  for (int i=n; i>0; i--) {
    for (int j=1; j<=n; j++) {
      if (g[j][nodes[i].id]) {
        net.addSide(++net.n, ind[j], nodes[i].a);
        net.addSide(nodes[i].id+2, net.n, inf);
        ind[j] = net.n;
      }
    }
  }

  net.init();
}

int main() {
#ifdef DEBUG
  freopen("src.in", "r", stdin);
  freopen("src.out", "w", stdout);
#endif

  static bool g[maxn+1][maxn+1];
  static Node nodes[maxn+1];
  int n, m;
  scanf("%d %d", &n, &m);
  for (int i=1; i<=n; i++) {
    scanf("%d", &nodes[i].a);
    nodes[i].id = i;
  }
  for (int i=1; i<=n; i++) scanf("%d", &nodes[i].b);
  for (int i=1; i<=m; i++) {
    int u, v;
    scanf("%d %d", &u, &v);
    g[u][v] = true;
  }

  static Network net;
  buildNet(n, nodes, g, net);

  ll ans=0;
  for (; !net.end; ans+=net.run(1, 2, inf));
  printf("%lld", ans);

  fclose(stdin);
  fclose(stdout);
  return 0;
}
